package com.ytjj.quartz.service;

import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.mall.model.DataStatistics;
import com.ytjj.quartz.dao.DataStatisticsDao;
import com.ytjj.quartz.dao.OrdersDao;
import com.ytjj.quartz.dao.ProductDao;
import com.ytjj.quartz.dao.UsersDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 *
 * @Version: 1.0
 */
@Service
@Slf4j
public class DataStatisticsService {

    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private UsersDao usersDao;

    @Autowired
    private DataStatisticsDao dataStatisticsDao;

    @Transactional(rollbackFor = Exception.class)
    @Async
    public void updateData(String startTime, String endTime, String operation){

        //创建DataStatistics对象
        DataStatistics data = new DataStatistics();

        //今日订单总数
        Integer orderCount = ordersDao.selTodayOrderCount(startTime, endTime);
        data.setOrderCount(orderCount);
        //今日销售总额
        BigDecimal salesTotal = ordersDao.selTodaySaleCount(startTime, endTime);
        if (salesTotal == null) {
            salesTotal = BigDecimal.ZERO;
        }
        data.setSalesTotal(salesTotal);
        //今日销售商品成本
        BigDecimal costPriceToday = ordersDao.getCostPriceToDay(startTime, endTime);
        if (costPriceToday == null) {
            costPriceToday = BigDecimal.ZERO;
        }
        data.setCostPriceToday(costPriceToday);
        //今日转发总Uv
        Integer forwardUv = ordersDao.getForwardUvCount(startTime, endTime);
        data.setForwardUv(forwardUv);

        //待付款订单
        Integer waitPayOrder = ordersDao.getOrderNumByStatus(0, startTime, endTime);
        data.setWaitPayOrder(waitPayOrder);
        //待发货订单
        Integer waitDeliverOrder = ordersDao.getOrderNumByStatus(1, startTime, endTime);
        data.setWaitDeliverOrder(waitDeliverOrder);
        //已发货订单
        Integer eliverdedOrder = ordersDao.getOrderNumByStatus(2, startTime, endTime);
        data.setEliverdedOrder(eliverdedOrder);
        //已完成订单
        Integer finishOrder = ordersDao.getOrderNumByStatus(3, startTime, endTime);
        data.setFinishOrder(finishOrder);
        //已取消订单
        Integer cancelOrderNum = ordersDao.getOrderNumByStatus(4, startTime, endTime);
        data.setCancelOrderNum(cancelOrderNum);
        //退款订单-包含无效订单
        Integer invalid = ordersDao.getOrderNumByStatus(5, startTime, endTime);
        Integer refund = ordersDao.getOrderNumByStatus(6, startTime, endTime);
        Integer refundOrder = invalid + refund;
        data.setRefundOrderNum(refundOrder);


        //全部商品
        Integer productAll = productDao.getProductCount();
        data.setProductAll(productAll);
        //已上架商品
        Integer productUp = productDao.getProductCountByStatus(1);
        data.setProductUp(productUp);
        //已下架商品
        Integer productDown = productDao.getProductCountByStatus(2);
        data.setProductDown(productDown);


        //总用户数
        Integer usersAll = usersDao.getUsersCount();
        data.setUsersAll(usersAll);
        //今日新增用户数
        Integer todayUsers = usersDao.getTodayUsersNum(startTime, endTime);
        data.setTodayUsers(todayUsers);
        //今日新增-ios
        Integer iosAll = usersDao.getUsersNumBySource("ios", startTime, endTime);
        data.setIosAll(iosAll);
        //今日新增-android
        Integer androidAll = usersDao.getUsersNumBySource("android", startTime, endTime);
        data.setAndroidAll(androidAll);
        //今日新增-weapp
        Integer weappAll = usersDao.getUsersNumBySource("weapp", startTime, endTime);
        data.setWeappAll(weappAll);
        //今日新增-h5
        Integer hAll = usersDao.getUsersNumBySource("h5", startTime, endTime);
        data.sethAll(hAll);
        //今日支付订单-ios
        Integer iosPayAll = ordersDao.getTodayPayNumBySource("ios", startTime, endTime);
        data.setIosPayAll(iosPayAll);
        //今日支付订单-android
        Integer androidPayAll = ordersDao.getTodayPayNumBySource("android", startTime, endTime);
        data.setAndroidPayAll(androidPayAll);
        //今日支付订单-weapp
        Integer weappPayAll = ordersDao.getTodayPayNumBySource("weapp", startTime, endTime);
        data.setWeappPayAll(weappPayAll);
        //今日支付订单-h5
        Integer hPayAll = ordersDao.getTodayPayNumBySource("h5", startTime, endTime);
        data.sethPayAll(hPayAll);

        String preTime = null;
        if (operation.equals("fiveFlush")) {
            //获取前一天时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            int preDay = cal.get(Calendar.DATE) - 1;
            preTime = year + "-" + (month < 10 ? "0" + month : month) + "-" + (preDay < 10 ? "0" + preDay : preDay) + " 00:00:00";
        } else if (operation.equals("onlyTwoFlush")) {
            //获取前一天时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            int preDay = cal.get(Calendar.DATE) - 2;
            preTime = year + "-" + (month < 10 ? "0" + month : month) + "-" + (preDay < 10 ? "0" + preDay : preDay) + " 00:00:00";
        } else {
            throw new ApiException("数据统计定时器异常，请检查！");
        }

        //昨日新增用户数
        Integer toYesterdayUsers = usersDao.getTodayUsersNum(preTime, startTime);
        data.setToYesterdayUsers(toYesterdayUsers);
        //昨日新增-ios
        Integer iosYestAll = usersDao.getUsersNumBySource("ios", preTime, startTime);
        data.setIosYestAll(iosYestAll);
        //昨日新增-android
        Integer androidYestAll = usersDao.getUsersNumBySource("android", preTime, startTime);
        data.setAndroidYestAll(androidYestAll);
        //昨日新增-weapp
        Integer weappYestAll = usersDao.getUsersNumBySource("weapp", preTime, startTime);
        data.setWeappYestAll(weappYestAll);
        //昨日新增-h5
        Integer hYestAll = usersDao.getUsersNumBySource("h5", preTime, startTime);
        data.sethYestAll(hYestAll);
        //昨日支付订单-ios
        Integer iosYestPayAll = ordersDao.getTodayPayNumBySource("ios", preTime, startTime);
        data.setIosYestPayAll(iosYestPayAll);
        //昨日支付订单-android
        Integer androidYestPayAll = ordersDao.getTodayPayNumBySource("android", preTime, startTime);
        data.setAndroidYestPayAll(androidYestPayAll);
        //昨日支付订单-weapp
        Integer weappYestPayAll = ordersDao.getTodayPayNumBySource("weapp", preTime, startTime);
        data.setWeappYestPayAll(weappYestPayAll);
        //昨日支付订单-h5
        Integer hYestPayAll = ordersDao.getTodayPayNumBySource("h5", preTime, startTime);
        data.sethYestPayAll(hYestPayAll);


        DataStatistics dataStatistics = dataStatisticsDao.getDataStatistics(startTime);
        if (operation.equals("fiveFlush")) {
            if (dataStatistics == null) {
                data.setCreateTime(new Date());
                dataStatisticsDao.insertData(data);
            } else {
                data.setRecordTime(new Date());
                data.setId(dataStatistics.getId());
                dataStatisticsDao.updateData(data);
            }
        } else if (operation.equals("onlyTwoFlush")) {
            data.setId(dataStatistics.getId());
            data.setRecordTime(new Date());
            dataStatisticsDao.updateData(data);
        } else {
            throw new ApiException("数据统计定时器异常，请检查！");
        }

    }

}
